package com.mc.moring.BafBase;

import android.app.Application;
import android.content.Context;

import com.mc.moring.app.Constants;
import com.squareup.picasso.OkHttp3Downloader;
import com.squareup.picasso.Picasso;
import com.xcjr.lib.net.https.SSLSocketClient;
import com.xcjr.lib.net.https.SSLTrustAllManager;

import java.util.Collections;

import okhttp3.OkHttpClient;
import okhttp3.Protocol;

/**
 * MyApplication
 * author: Created by nao on 2019/7/10
 * version: 1.0.0
 */
public class MyApplication extends Application {

    private static MyApplication instance;
    public static Context applicationContext;

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        applicationContext = this;

        //图片
        configPicasso();
    }

    /**
     * 配置 Picasso
     */
    private void configPicasso() {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        OkHttpClient client = null;
        try {
            client = builder.hostnameVerifier(SSLSocketClient.getHostnameVerifier())
                    .sslSocketFactory(SSLSocketClient.getSSLSocketFactory(), new SSLTrustAllManager())
                    .protocols(Collections.singletonList(Protocol.HTTP_1_1)).build();
        } catch (Exception e) {
            e.printStackTrace();
        }
        final Picasso picasso = new Picasso.Builder(this).downloader(new OkHttp3Downloader(client)).build();
        Picasso.setSingletonInstance(picasso);
    }

    public static MyApplication getInstance() {
        return instance;
    }

    /**
     * 通过 DaoMaster 的内部类 DevOpenHelper，你可以得到一个便利的 SQLiteOpenHelper 对象。
     * 可能你已经注意到了，你并不需要去编写「CREATE TABLE」这样的 SQL 语句，因为 greenDAO 已经帮你做了。
     * 注意：默认的 DaoMaster.DevOpenHelper 会在数据库升级时，删除所有的表，意味着这将导致数据的丢失。
     */

    // 过度
    public boolean isFirst() {
        return PreferencesUtil.getBoolean(this, Constants.preferencesKey.IS_FIRST_KEY, true);
    }

    // 是否已经登录。登录了返回true，否则返回false
    public boolean isLogin() {
        return PreferencesUtil.getBoolean(this, Constants.preferencesKey.IS_LOGIN_KEY, false);
    }

    public boolean isWelcome() {
        return PreferencesUtil.getBoolean(this, Constants.preferencesKey.IS_WELCOME_KEY, false);
    }

    // 获取登录用户id
    public String getUserId() {
        return PreferencesUtil.getString(this, Constants.preferencesKey.LOGIN_USER_ID_KEY, "");
    }

    public String getUserName() {
        return PreferencesUtil.getString(this, Constants.preferencesKey.LOGIN_USER_NAME_KEY, "");
    }
}
